/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.lucidhex;

import com.lucidhex.hex.LucidBoardBounds;
import com.lucidhex.hex.LucidHex;
import com.lucidhex.hex.LucidHexType;
import com.lucidhex.hex.LucidIndex;
import com.lucidhex.hex.SuperHexBoard;
import com.lucidhex.io.LucidWorldParser;
import com.lucidhex.util.DualMap;
import com.lucidhex.viewer.ViewRenderer;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Wolf
 * Hexes are stored in axial coordinate System
 * Sometimes they are converted into "cube system" for distance calculations  ErfCoord Takes care of that
 */
public class LucidBoard implements LucidXMLObject{
    private HashMap<String,LucidHexType> hexTypes = new HashMap<>();
    private DualMap<LucidIndex, LucidHex> board = new DualMap<>();
    private LucidBoardBounds bounds = new LucidBoardBounds();
    private SuperHexBoard bigHex = null;
    private LucidWorld world = null;
    public void setWorld(LucidWorld aWorld){
        world = aWorld;
        hexTypes.putAll(world.getDefaults().getDefaultHexTypes());
        
    }
    public void addHex(LucidIndex index, LucidHex aHex) {
        aHex.setBoard(this);
        board.put(index, aHex);
        bounds.checkBounds(index);
    }

    public LucidIndex getPosition(LucidHex aHex) {
        return board.reverseGet(aHex);
    }

    public LucidHex getHexAt(LucidIndex aCoord) {
        LucidHex out = board.get(aCoord);
        return out;
    }
    public Set<LucidHex> getAvailbleHexes(){
        return board.getValues();
    }

    public LucidBoardBounds getBounds() {
        return bounds;
    }

    public boolean isEmpty() {
        return board.isEmpty();
    }

    public void paint(ViewRenderer pencil) {
        for(LucidHex eh : board.getValues()){
            pencil.drawHex(eh);
        }
        for(LucidHex eh : board.getValues()){
            //pencil.drawHexLabels(eh);
        }
        
    }

    @Override
    public String getTag() {
        return "LucidBoard";
    }

    @Override
    public void getChildren(Collection<Object> children) {
        // order important Hex Types first
        children.addAll(hexTypes.values());
        children.addAll(board.getValues());
    }

    @Override
    public void addField(String id, String attribute) {
        
    }

    @Override
    public void getFields(Map<String, String> attrib) {
        
    }
    @Override
    public void addChild(Object aChild) {
        // dont need this board is set at inital creation
        //LucidHex aHex = (LucidHex)aChild;
        //this.addHex(aHex.getPosition(), aHex);
    }
   @Override
    public boolean writeToXml() {
        return true;
    }

    public void clear() {
        board.clear();
    }

    public void resizeMap(int r, LucidHexType type) {
        for(int a = -r; a <= r; a++){
            for(int c = -r; c <= r; c++){
                if(Math.abs(-a-c) <= r){
                    LucidIndex ei = new LucidIndex(a, c);
                    if(board.get(ei)== null){
                        this.addHex(ei, type.makeNewHex(this));
                    }
                }
            }
        }
    }

    public void removeHex(LucidIndex ei) {
        board.remove(ei);
        bounds.checkBoundsRemoval(ei);
    }

    public Collection<LucidHexType> getAvailbleHexTypes() {
        return hexTypes.values();
    }

    public LucidHexType getHexType(String typeName) {
        LucidHexType lht = hexTypes.get(typeName);
        if(lht == null){
            lht = LucidHexType.NULL_HEX;
        }
        return lht;
    }

    @Override
    public void parseLink(LucidWorldParser parser) {
        parser.getParsingWorld().setBoard(this);
        this.setWorld(parser.getParsingWorld());
    }
}
